//
//  HSViewController.m
//  悦享
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 张洪国. All rights reserved.
//

#import "HSViewController.h"
#import "MusicCollectionViewCell.h"
#import "MusicModel.h"
#import <MJRefresh.h>
#import "AllMusicViewController.h"
#import "CY_DetailViewController.h"
#import <MBProgressHUD.h>

@interface HSViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *musicCollection;
/// 保存数据的数组
@property (nonatomic, retain) NSMutableArray *arrData;
/// 控制数据页数
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isUpState;
/// 人们歌曲的按钮
@property (nonatomic, retain) UIButton *hotButton;
/// 最新歌曲按钮
@property (nonatomic, retain) UIButton *newestButton;
/// collectionView 头视图
@property (nonatomic, retain) UIView *headerView;
/// 查看全部按钮
@property (nonatomic, retain) UIButton *allButton;
/// 进入页面加载菊花
@property (nonatomic, retain) MBProgressHUD *progressView;

@end

@implementation HSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    self.navigationItem.title = @"歌单";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor whiteColor]}];

    self.view.backgroundColor = [UIColor whiteColor];
    self.arrData = [NSMutableArray array];
    
    self.type = @"最热";
    
    /// 创建collectionView
    [self createTableView];
    /// 数据处理
    [self dataHandle];

    // 下拉刷新
    [self addHeader];
    // 上拉加载
    [self addFooter];
    
    _page = 1;
    _isUpState = NO;
    
}

// 下拉刷新
- (void)addHeader {
    
    //    NSMutableArray *arr = [NSMutableArray array];
    //    for (NSInteger i = 1; i < 4; i++) {
    //        NSString *str = [NSString stringWithFormat:@"dropdown_loading_0%ld@2x.png", (long)i];
    //        UIImage *img = [UIImage imageNamed:str];
    //        [arr addObject:img];
    //    }
    //    //    [header setImages:arr forState:MJRefreshStateRefreshing];
    //    [header setImages:arr duration:2 forState:MJRefreshStateRefreshing];
    //    [header setImages:arr duration:1 forState:MJRefreshStatePulling];
    //    [header setImages:arr duration:1 forState:MJRefreshStateIdle];
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        _page = 1;
        _isUpState = NO;
        [self dataHandle];
    }];
    
    self.musicCollection.mj_header = header;
}

// 上拉加载
- (void)addFooter {

//    _page++;
//    self.musicCollection.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(dataHandle)];
    
    self.musicCollection.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _isUpState = YES;
        _page++;
        [self dataHandle];
//        NSLog(@"%ld", _page);
    }];
    
    _musicCollection.mj_footer.automaticallyHidden = YES;

}

#pragma mark - 创建歌单首页collectionView
- (void)createTableView {
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SREEN_WIDTH, 50)];
    [self.view addSubview:_headerView];
    
    self.allButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _allButton.frame = CGRectMake(15, 10, 70, 30);
    [_allButton setTitle:@"全部 >" forState:UIControlStateNormal];
    [_allButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    _allButton.layer.borderWidth = 2;
    _allButton.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [_headerView addSubview:_allButton];
    [_allButton addTarget:self action:@selector(allMusicAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.newestButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _newestButton.frame = CGRectMake(SREEN_WIDTH - 55, 10, 40, 30);
    [_newestButton setTitle:@"最新" forState:UIControlStateNormal];
    [_newestButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_headerView addSubview:_newestButton];
    [_newestButton addTarget:self action:@selector(newAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(SREEN_WIDTH - 66, 15, 1, 20)];
    line.backgroundColor = [UIColor grayColor];
    [_headerView addSubview:line];
    
    self.hotButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _hotButton.frame = CGRectMake(SREEN_WIDTH - 116, 10, 40, 30);
    [_hotButton setTitle:@"最热" forState:UIControlStateNormal];
    [_hotButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_headerView addSubview:_hotButton];
    [_hotButton addTarget:self action:@selector(hotAction:) forControlEvents:UIControlEventTouchUpInside];

    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake((SREEN_WIDTH - 30) / 2, (SREEN_WIDTH - 30) / 2 + 30);
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);
    
    
    self.musicCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, SREEN_WIDTH, SREEN_HEIGHT - 164) collectionViewLayout:layout];
    _musicCollection.backgroundColor = [UIColor whiteColor];
    _musicCollection.delegate = self;
    _musicCollection.dataSource = self;
    
    [self.view addSubview:_musicCollection];
    
    [_musicCollection registerClass:[MusicCollectionViewCell class] forCellWithReuseIdentifier:@"MusicCollectionViewCell"];
    
    self.progressView = [[MBProgressHUD alloc] initWithView:self.view];
    self.progressView.labelText = @"加载中...";
    [self.progressView setMode:MBProgressHUDModeIndeterminate];
    [self.progressView show:YES];
    self.progressView.backgroundColor = [UIColor colorWithRed:0.55 green:0.55 blue:0.55 alpha:0.5];
    [self.view addSubview:_progressView];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _arrData.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MusicCollectionViewCell" forIndexPath:indexPath];
    
    MusicModel *model = _arrData[indexPath.row];
    cell.model = model;
    
    return cell;
    
}

// collectionView 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CY_DetailViewController *detailVc = [[CY_DetailViewController alloc] init];
    
    detailVc.hidesBottomBarWhenPushed = YES;
    MusicModel *model = _arrData[indexPath.row];
    detailVc.urlString = model.pic_url;
    detailVc.musicID = model._id;
    detailVc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:detailVc animated:YES completion:^{
        
    }];
    
}



#pragma mark - 最热歌单数据处理
- (void)dataHandle {
    
    NSString *path = [NSString stringWithFormat:@"http://search.dongting.com/songlist/search?q=tag:%@&page=%ld&size=10", _type, _page];
    
    [APPTools GETWithURL:path par:nil success:^(id responseObject) {

        
        if (_isUpState == NO) {
            
            [_arrData removeAllObjects];
            
        }
        
        NSArray *tempArr = [MusicModel baseModelHandleWithArr:responseObject[@"data"]];
        [_arrData addObjectsFromArray:tempArr];


        [_musicCollection reloadData];

        [_musicCollection.mj_footer endRefreshing];
        [_musicCollection.mj_header endRefreshing];
        
        
        [_progressView removeFromSuperview];
        
    } filed:^(NSError *error) {
        
    }];
    
}

#pragma mark - 全部歌单按钮点击方法
- (void)allMusicAction:(UIButton *)button {
    
    AllMusicViewController *allMusicVc = [[AllMusicViewController alloc] init];
    
    allMusicVc.block = ^(NSString *str) {
      
        _type = str;
        
        NSString *themeStr = [NSString stringWithFormat:@"%@ >", _type];
        
        [_allButton setTitle:themeStr forState:UIControlStateNormal];
        
        [_allButton addTarget:self action:@selector(allMusicAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self dataHandle];
    };
    
    [self.navigationController pushViewController:allMusicVc animated:YES];
    
}
#pragma mark - 最热按钮点击方法
- (void)hotAction:(UIButton *)button {
    
    [_hotButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_newestButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_allButton setTitle:@"全部 >" forState:UIControlStateNormal];

    _type = @"最热";
    _page = 1;
    [_arrData removeAllObjects];
    
    [_musicCollection reloadData];
    [self dataHandle];

    
}

#pragma mark - 最新按钮点击方法
- (void)newAction:(UIButton *)button {
    
    [_hotButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_newestButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_allButton setTitle:@"全部 >" forState:UIControlStateNormal];

    _type = @"最新";
    _page = 1;
    [_arrData removeAllObjects];
    [_musicCollection reloadData];
    [self dataHandle];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
